<?php
/**
 * News_Controller
 * - Controller for module page Home
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_CORE_DIR . "/Paging.class.php");
require_once(LIB_DAO_DIR . "/MNews.class.php");

class News_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuNews = true;
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'News';
        $this->arrNewsCategory = array();
        $this->arrField = array();
        $this->flagAdd = false;
        $this->arrContent = array();
		$this->currPage = '';
		$this->numberPage = '';
		$this->strPaging = '';
        $this->tpl_title_list = 'Manager News';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $idcKM = 1;
        $idcXH = 2;
        
        $this->idc = isset($_GET['idc']) ? $_GET['idc'] : '';
        if ($this->idc == $idcKM) {
        	$this->flagNewKm = true;
        } else {
        	$this->flagNewXh = true;
        }
        
        if ($this->idc != '') {	
        	$ojbMNews = new MNews();
        	if ($this->isPost()) {
        		$arrField = FunctionCommon::getPost(array('itemID', 'mode', 'checkdel'));
		        // Delete by id
		        if ($arrField['mode'] == 'delete') {
		        	$dataSanpham = $ojbMNews->getByPrimaryKey($arrField['itemID']);
		        	$ojbMNews->delete(array('id' => $arrField['itemID']));
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			    	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
		        }
		        
		        // Delete all by array id
		    	if ($arrField['mode'] == 'deleteall') {
		    		if (count ($arrField['checkdel']) > 0) {
		    			foreach ($arrField['checkdel'] as $aP) {
		    				$dataSanpham = $ojbMNews->getByPrimaryKey($aP);
		        			$ojbMNews->delete(array('id' => $aP));
		        			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
			    			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
		    			}
		    		}
		        }
        	}
        	
        	// List all
		    $arrParam = array('m' => 'news','idc'=>$this->idc);
		    $arrField = array('id', 'seq', 'idc', 'tieude', 'luotxem', 'update_at');
		    $condition = array("idc"=>array('value'=>$this->idc, 'math'=>'='));
			
		    $data = $this->paging($arrParam, $arrField, $condition);
		    $arrContent = $data["data"];
			if (count ($arrContent) > 0) {
				foreach ($arrContent as $content) {
					$this->arrContent[] = $content;
				}
				$this->currPage = $data["currPage"];
				$this->numberPage = $data["numberPage"];
				$this->strPaging = $data["strPaging"];
			}
        	$this->flagAdd = true;
        }

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }

	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = 'Thêm tin mới';
        $this->tpl_button = "Thêm mới";
        $this->arrField = array();
        $this->tpl_title_list = 'Manager news';
        $this->dialog = false;
        $this->id = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $idcKM = 1;
        $idcXH = 2;
        
        $this->idc = isset($_GET['idc']) ? $_GET['idc'] : '';
    	if ($this->idc == $idcKM) {
        	$this->flagNewKm = true;
        } else {
        	$this->flagNewXh = true;
        }
        $this->arrField = FunctionCommon::getPost(array('tieude', 'tomtat', 'noidung', 'nameimage', 'itemID', 'mode', 'type'));
        
        if ($this->isPost()) {
        	$ojbNew = new MNews();       	
        	$this->id = $this->arrField['itemID'];
        	
        	if ($this->arrField['mode'] != '') {
        		
        		// Update du lieu
        		if ($this->arrField['type'] == 'edit' && $this->id != '') {
	        		$ojbNew->setAttribute('tieude', $this->arrField['tieude']);
	        		$ojbNew->setAttribute('tomtat', $this->arrField['tomtat']);
	        		$ojbNew->setAttribute('noidung', $this->arrField['noidung']);
	        		$ojbNew->setAttribute('code', $this->arrField['nameimage']);
		        	$ojbNew->update(array('id' => $this->id));
		        	$this->dialog = true;
        		}
        		
        		// Insert du lieu
	        	if ($this->arrField['type'] == 'edit' && $this->id == '') {
		    		$ojbNew->setAttribute('seq', $ojbNew->getMinMax(array(), true) + 1);
		    		$ojbNew->setAttribute('idc', $this->idc);
			        $ojbNew->setAttribute('tieude', $this->arrField['tieude']);
	        		$ojbNew->setAttribute('tomtat', $this->arrField['tomtat']);
	        		$ojbNew->setAttribute('noidung', $this->arrField['noidung']);
	        		$ojbNew->setAttribute('code', $this->arrField['nameimage']);
			        $ojbNew->setAttribute('insert_at', date('Y-m-d H:i:s'));
		        	$ojbNew->insert();
		        	$this->dialog = true;
		        }
		        
        		// Xu ly file
	        	if ($this->arrField['type'] == 'upload') {
	        		$this->tenFile = FunctionCommon::uploadFileTo($_FILES["code"], "img-the", $this->arrField['nameimage']);
	        		if ($this->tenFile == false) {
	        			$this->tenFile = $this->arrField['nameimage'];
	        		}
		        }
		        
		        // Delete image
	        	if ($this->arrField['type'] == 'delete') {
		        	$tenFile = $this->arrField['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$tenFile);
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$tenFile);
		        }
        	} else {
        		$this->tpl_title = 'Sửa tin tức';
        		$this->tpl_button = "Sửa";
	        	$this->arrField = $ojbNew->getByPrimaryKey($this->id);
	        	$this->tenFile = $this->arrField['code'];
        	}
        }

        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
    * paging
    * - Array $arrParam
    * - Array $condition
    */
	public function paging($arrParam = null, $arrField = array(), $condition = array()) {
		$objPaging = new Paging();
		$ojbMNew = new MNews();
	    // Get total record of content menu
		$totalRecord = $ojbMNew->getTotalData($condition);
		 // Get page current
        if (isset($_REQUEST["pageNo"])) {
        	$currentPage = $_REQUEST["pageNo"];
        }
        else {
            $currentPage = 1;
        }
		$numberRecord = constants::$numberPageListMNews;
        // get page number
		if (!is_null($numberRecord) && $numberRecord != 0) {
			$numberPage = ceil($totalRecord / $numberRecord);
		} else {
			$numberPage = 0;
		}
		// get start record
		$startRecord = ($currentPage - 1) * $numberRecord;

        // get array paging
		$arrPage = $objPaging->getArrPaging($currentPage, $numberPage);
		// get string page
		$strPaging = $objPaging->getStrPaging($arrPage, $currentPage, $numberPage, $arrParam);

        // Get all content menu
        $content = $ojbMNew->getListData($startRecord, $numberRecord, $arrField, $condition);
        $no = $startRecord + 1;
		$result = array("total"=>$totalRecord,
						"data"=>$content,
						"currPage"=>$currentPage,
						"numberPage"=>$numberPage,
						"strPaging"=>$strPaging,
						"no"=>$no);

		return $result;
	}
}
?>

